/**
 * MemoryManager.java
 * Copyright (C) 2011 Yuanyan<yuanyan.cao@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * For information on how to redistribute this software under
 * the terms of a license other than GNU General Public License
 * contact yuanyan.cao@gmail.com
 */
package edu.rabbit.kernel.memory;

import edu.rabbit.kernel.MemoryBufferType;
import edu.rabbit.kernel.DbUtility;

/**
 * @author Yuanyan<yanyan.cao@gmail.com>
 * 
 * 
 */
public class MemoryManager implements IMemoryManager {

    private MemoryBufferType defaultBufferType = DbUtility.getEnumSysProp(
            "MemoryManager.defaultBufferType", MemoryBufferType.ARRAY);

    /*
     * (non-Javadoc)
     * 
     * @see edu.rabbit.kernel.memory.IMemoryManager#
     * getDefaultBufferType()
     */
    public MemoryBufferType getDefaultBufferType() {
        return defaultBufferType;
    }

    /*
     * (non-Javadoc)
     * 
     * @seeedu.rabbit.kernel.memory.IMemoryManager#
     * setDefaultBufferType
     * (edu.rabbit.kernel.memory.IMemoryManager
     * .BufferType)
     */
    public void setDefaultBufferType(final MemoryBufferType bufferType) {
        if (bufferType != null) {
            defaultBufferType = bufferType;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.internal.IMemoryManager#allocatePtr(int)
     */
    public IMemoryPointer allocatePtr(int size) {
        return allocate(size).getPointer(0);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.internal.IMemoryManager#allocatePtr(int,
     * org.tmatesoft.rabbit.core.internal.MemoryBufferType)
     */
    public IMemoryPointer allocatePtr(int size, MemoryBufferType bufferType) {
        return allocate(size, bufferType).getPointer(0);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * edu.rabbit.kernel.internal.memory.IMemoryManager
     * #allocate(int)
     */
    public IMemoryBuffer allocate(final int size) {
        return allocate(size, defaultBufferType);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * edu.rabbit.kernel.memory.IMemoryManager#allocate
     * (int,
     * edu.rabbit.kernel.memory.IMemoryManager.BufferType)
     */
    public IMemoryBuffer allocate(int size, MemoryBufferType bufferType) {
        if (size >= 0) {
            final IMemoryBuffer buffer;
            switch (bufferType) {
            case ARRAY:
                buffer = new ByteArrayBuffer();
                break;
            case BUFFER:
                buffer = new DbByteBuffer();
                break;
            case DIRECT:
                buffer = new DirectByteBuffer();
                break;
            default:
                buffer = new ByteArrayBuffer();
            }
            buffer.allocate(size);
            return buffer;
        } else {
            return null;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * edu.rabbit.kernel.internal.memory.IMemoryManager
     * #free
     * (edu.rabbit.kernel.internal.memory.IMemoryBuffer)
     */
    public void free(final IMemoryBuffer buffer) {
        if (buffer != null) {
            buffer.free();
        }
    }

}
